﻿using System.Linq;
using CrateMayhem.Common.Model;
using CrateMayhem.Common.Services;
using CrateMayhem.Common.ViewModel;
using CrateMayhem.WinRT.Controls;
using Windows.UI.Xaml.Navigation;

namespace CrateMayhem.WinRT
{
    public sealed partial class MainPage
    {
        private readonly IGameSettingsService gameSettingsService;

        public MainPage()
        {
            InitializeComponent();
            DataContext = Global.Container.Resolve<ViewModelBase>(typeof (GameViewModel).Name);
            var serviceFactory = Global.Container.Resolve<IServiceFactory>();
            gameSettingsService = serviceFactory.Create<IGameSettingsService>();
            gameSettingsService.ControlsSwitched += (s, e) => LayoutControls();
            LayoutControls();
#if DEBUG
            Loaded += (s, e) =>
                {
                    var panel = new Windows.UI.Xaml.Controls.StackPanel();
                    var button = new Windows.UI.Xaml.Controls.Button();
                    button.Content = "Simulator";
                    button.Click += OnSimulatorClick;
                    panel.Children.Add(button);
                    var grid = (Windows.UI.Xaml.Controls.Grid)Content;
                    grid.Children.Add(panel);
                    Windows.UI.Xaml.Controls.Grid.SetRow(panel, 2);
                };
#endif
        }

        private void LayoutControls()
        {
            var location = gameSettingsService.GetControlsLocation();

            var grid = Layout.Children.First(c => c is GameGrid);

            if (location == ControlsLocationType.Left && Layout.Children.IndexOf(grid) == 0)
            {
                Layout.Children.Remove(grid);
                Layout.Children.Add(grid);
            }
            else if (location == ControlsLocationType.Right && Layout.Children.IndexOf(grid) == 1)
            {
                Layout.Children.Remove(grid);
                Layout.Children.Insert(0, grid);
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

#if DEBUG
        private async void OnSimulatorClick(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var sf = Global.Container.Resolve<IServiceFactory>();
            var gs = sf.Create<IGameService>();
            var model = (GameViewModel)DataContext;
            var wr = Global.Container.Resolve<CrateMayhem.Common.Data.IWorldRepository>();
            var w = wr.Find(model.Level);

            foreach (var m in w.Solution.ToLower())
            {
                MoveType move;
                if (m.ToString() == 'd'.ToString()) move = MoveType.Down;
                else if (m.ToString() == 'u'.ToString()) move = MoveType.Up;
                else if (m.ToString() == 'l'.ToString()) move = MoveType.Left;
                else move = MoveType.Right;
                gs.RaisePush(move);
                await System.Threading.Tasks.Task.Delay(10);
            }
        }
#endif
    }
}
